package com.asion.qkan.adapters;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.asion.qkan.R;
import com.asion.qkan.entry.MaterialBean;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by HCJ on 2016/10/17.
 */

public class CommonRecyAdapter<D> extends RecyclerView.Adapter<CommonRecyAdapter.ViewHolder>{
    private Context context;
    private List<D> list;

    public CommonRecyAdapter(Context context) {
        this.context = context;
        list = new ArrayList<D>();
    }

    public void addAll(Collection<? extends D> collection){
        list.addAll(collection);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        D d = list.get(position);
        if(d instanceof MaterialBean){
            return 0;
        }
        return super.getItemViewType(position);
    }
    
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater from = LayoutInflater.from(context);
        View view = null;
        if(viewType == 0){
            view = from.inflate(R.layout.item_material, parent, false);
        }
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        if(itemViewType == 0){
            MaterialBean.DataBean.TopicsBean topicsBean = (MaterialBean.DataBean.TopicsBean) list.get(position);
            holder.desc.setText(topicsBean.getDescription());
            holder.title.setText(topicsBean.getTitle());
            holder.imageView.setImageURI(Uri.parse(topicsBean.getCover_image_url()));
        }
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private SimpleDraweeView imageView;
        private TextView title,desc;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (SimpleDraweeView) itemView.findViewById(R.id.item_material_img);
            title = (TextView) itemView.findViewById(R.id.item_material_title);
            desc = (TextView) itemView.findViewById(R.id.item_material_desc);
        }
    }
}
